package com.tzsp.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tzsp.reggie.commons.dto.SetmealDto;
import com.tzsp.reggie.commons.utils.BusinessException;
import com.tzsp.reggie.domain.Setmeal;
import com.tzsp.reggie.domain.SetmealDish;
import com.tzsp.reggie.mapper.SetmealMapper;
import com.tzsp.reggie.service.SetmealDishService;
import com.tzsp.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     *保存套餐信息，包括套餐关联表Setmeal_dish
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithSetmealDish(SetmealDto setmealDto) {
        // 保存套餐信息，setmeal 执行insert操作
        this.save(setmealDto);


        // 保存套餐关联表信息，setmeal_dish 执行insert操作
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 根据id获取套餐和套餐关联表信息，展示在修改套餐页面
     * @param id
     * @return
     */
    @Override
    public SetmealDto querySetmealInfoById(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);


        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getId()!=null,SetmealDish::getSetmealId,setmeal.getId());

        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    /**
     * 对套餐信息进行修改（包括修改套餐关联表信息）
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithSetmealDish(SetmealDto setmealDto) {
        //修改套餐表中的信息
        this.updateById(setmealDto);

        //删除套餐表中套餐菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmealDto.getId()!=null,SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //获取套餐产菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //获取到的套餐菜品信息只有dishId和name，copies，price，缺少setmeal_id
        setmealDishes = setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //添加套餐菜品信息
        setmealDishService.saveBatch(setmealDishes);


    }

    /**
     * 根据ids删除对应的套餐信息（包括修改套餐关联表信息）
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithSetmealDish(List<Long> ids) {
        //删除套餐表中的信息
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.in(ids!=null,Setmeal::getId,ids);
        List<Setmeal> setmealList = this.list(setmealQueryWrapper);
        if (setmealList!=null){
            for (Setmeal setmeal:setmealList){
                if (setmeal.getStatus()==0){
                    this.removeByIds(ids);
                }else {
                    throw new BusinessException("当前菜品处于售卖中，不允许删除！！");
                }
            }
        }
        //删除套餐菜品信息
        LambdaQueryWrapper<SetmealDish> setmealDishQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishQueryWrapper.in(ids!=null,SetmealDish::getSetmealId,ids);
        setmealDishService.remove(setmealDishQueryWrapper);
    }

    @Override
    public void editWithSetmealDish(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids!=null,Setmeal::getId,ids);
        List<Setmeal> setmealList = this.list(queryWrapper);
        if (setmealList!=null){
            for (Setmeal setmeal:setmealList){
                setmeal.setStatus(status);
                this.updateById(setmeal);
            }
        }

    }
}
